<?php
class IndexController extends Zend_Controller_Action 
{
	private $m_AppUI; // bien luu thong tin dang nhap

	public function init()
	{
		$this->AppUI = Zend_Auth::getInstance()->getIdentity();
	}

	public function syncresumesAction()
	{
		header('Content-Type: text/html; charset=utf-8');
		$db = Zend_Registry::get('db');
		$objCommon = Application_Model_Common::getInstance();
		$arrResumes = $db->select()->from('resumes_tn')->query()->fetchAll();
		foreach($arrResumes as $id=>$resume)
		{
			foreach($resume as $k=>$v)
			{
				$resume[$k] = utf8_decode($resume[$k]);
				$resume[$k] = iconv("UTF-8","UTF-8//IGNORE//TRANSLIT",$resume[$k]);
//				$resume[$k] = utf8_decode($v);
			}

			$data = array(
				'RESUME_TITLE' => $resume['RESUME_TITLE'],
				'EXPERIENCES' => $resume['EXPERIENCES'],
				'EDUCATIONS' => $resume['EDUCATIONS'],
				'OBJECTIVE' => $resume['OBJECTIVE'],
				'SKILLS' => $resume['SKILLS'],
				'REFERENCES' => $resume['REFERENCES']
			);
			$db->update('resumes', $data, 'resume_id='.$resume['RESUME_ID']);
		}
		die('finish');
		debug($arrResumes, true);
		$arrLocs = $objCommon->getLocations();
		$arrIns = $objCommon->getIndustries();
		$arrLevels = $objCommon->getJobLevel();
		$arrDegrees = $objCommon->getJobDegree();
		$arrLevelIds = array_keys($arrLevels);
		$arrLocIds = array_keys($arrLocs);
		$arrInIds = array_keys($arrIns);
		$arrDegIds = array_keys($arrDegrees);
		$nLocIds = count($arrLocIds);
		$nIndIds = count($arrInIds);
		$nLelIds = count($arrLevels);
		$nDegIds = count($arrDegrees);

		foreach($arrResumes as $resume)
		{
			$ids = explode(',', $resume['LOCATIONS']);
			$arrIds = array();
			$data = array();
			foreach($ids as $id)
			{
				if(!isset($arrLocs[$id])) $id = $arrLocIds[rand(0, $nLocIds)];
				$arrIds[$id] = $id;
			}
			$data['LOCATIONS'] = implode(',', $arrIds);
			
			$ids = explode(',', $resume['INDUSTRIES']);
			$arrIds = array();
			foreach($ids as $id)
			{
				if(!isset($arrIns[$id]))  $id = $arrInIds[rand(0, $nIndIds)];
				$arrIds[$id] = $id;
			}
			$data['INDUSTRIES'] = implode(',', $arrIds);
			if($resume['SALARY_FROM'] < 1 && $resume['SALARY_TO'] < 1)
				$data['SALARY_UNIT'] = 'ltt';
			
			if(!isset($arrLevels[ $resume['LEVEL_ID'] ]))
				$data['LEVEL_ID'] = $arrLevelIds[ rand(0, $nLelIds) ];
			else
				$data['LEVEL_ID'] = $resume['LEVEL_ID'];
			$data['CURRENT_LEVEL_ID' ] = $data['LEVEL_ID'];

			if(!isset($arrDegrees[ $resume['DEGREE_ID'] ]))
				$data['DEGREE_ID'] = $arrDegIds[ rand(0, $nDegIds) ];
			else
				$data['DEGREE_ID'] = $resume['DEGREE_ID'];
			$data['CURRENT_DEGREE_ID' ] = $data['DEGREE_ID'];

			$db->update('resumes', $data, 'resume_id='.$resume['RESUME_ID']);
		}
		die('finish');
	}
	
	public function syncAction()
	{
		$objJob = Application_Model_Jobs::getInstance();
		$objCommon = Application_Model_Common::getInstance();
		$arrJobs = $objJob->find(array('limit'=>2000), $total);
		$arrLocs = $objCommon->getLocations();
		$arrIns = $objCommon->getIndustries();
		$arrLocIds = array_keys($arrLocs);
		$arrInIds = array_keys($arrIns);
		$nLocIds = count($arrLocIds);
		$nIndIds = count($arrInIds);

		foreach($arrJobs as $job)
		{
			$ids = explode(',', $job['LOCATIONS']);
			$arrIds = array();
			foreach($ids as $id)
			{
				if(!isset($arrLocs[$id])) $id = $arrLocIds[rand(0, $nLocIds)];
				$arrIds[$id] = $id;
			}
			$job['LOCATIONS'] = implode(',', $arrIds);
			
			$ids = explode(',', $job['INDUSTRIES']);
			$arrIds = array();
			foreach($ids as $id)
			{
				if(!isset($arrIns[$id]))  $id = $arrInIds[rand(0, $nIndIds)];
				$arrIds[$id] = $id;
			}
			$job['INDUSTRIES'] = implode(',', $arrIds);
			$objJob->save($job);
		}
		die('finish');
	}

	public function indexAction()
	{
		$objCommon = Application_Model_Common::getInstance();
		$this->view->arrTopLocations = $objCommon->getLocations(LANGUAGE, 3, 1, 18);
		$this->view->arrTopIndustries = $objCommon->getIndustries(LANGUAGE, 3, 18);
		$this->view->arrLastestNews = Application_Model_News::getInstance()->getLastest('career', 6);
		$objJob = Application_Model_Jobs::getInstance();
		$this->view->objJob = $objJob;
		$this->view->arrTopJobs = $objJob->getJobHomePage();
		$this->view->arrLogos = Application_Model_Employer::getInstance()->getHomeLogos(14);
	}

	public function rssAction()
	{
	}

	public function contactAction()
	{
		if($this->_request->getPost('action')=='send')
		{
			$arrData = array(
				'object' => 'jobseeker',
				'created' => time(),
				'title' => $this->_request->getPost('title'),
				'name' => $this->_request->getPost('name'),
				'address' => $this->_request->getPost('address'),
				'email' => $this->_request->getPost('email'),
				'content' => $this->_request->getPost('content')
			);
			Application_Model_Common::getInstance()->addContact($arrData);
			$this->view->arrData = $arrData;
			$body = $this->view->fetch('index/mail.contact.tpl');
			Common::email();
			$this->_redirect(BASE_URL.'contact?action=success');
		}
		else
			$this->view->action = $this->_request->getParam('action');
	}
}